package dk.eclipse.android.badassapp.sensor.gyroscope;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import dk.eclipse.android.badassapp.R;
import dk.eclipse.android.badassapp.sensor.orientation.OrientationManager;

/**
 * This activity explores the sensor API and services.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 */
public class GyroscopeActivity extends Activity implements
		GyroscopeListener {
	private static final String NOT_AVAILABLE = "N/A";

	/**
	 * Tag used for logging information.
	 */
	private static final String LOG_TAG = GyroscopeActivity.class
			.getSimpleName();

	private static Context CONTEXT;

	private TextView gyroscopeXTextView;
	private TextView gyroscopeYTextView;
	private TextView gyroscopeZTextView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.sensor_gyroscope);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_app);

		gyroscopeXTextView = (TextView) findViewById(R.id.accelerometerX);
		gyroscopeYTextView = (TextView) findViewById(R.id.accelerometerY);
		gyroscopeZTextView = (TextView) findViewById(R.id.accelerometerZ);

		CONTEXT = this;

		String initValue = "0";
		if (!GyroscopeManager.isSupported()) {
			initValue = NOT_AVAILABLE;
		}
		gyroscopeXTextView.setText(initValue);
		gyroscopeYTextView.setText(initValue);
		gyroscopeZTextView.setText(initValue);

		initValue = "0";
		if (!GyroscopeManager.isSupported()) {
			initValue = NOT_AVAILABLE;
		}
	}

	protected void onResume() {
		super.onResume();
		if (GyroscopeManager.isSupported()) {
			GyroscopeManager.startListening(this);
		}

	}

	protected void onDestroy() {
		super.onDestroy();
		if (GyroscopeManager.isListening()) {
			GyroscopeManager.stopListening();
		}
	}

	public static Context getContext() {
		return CONTEXT;
	}



	/**
	 * onAccelerationChanged callback
	 */
	public void onGyroscopeChanged(float x, float y, float z) {
		gyroscopeXTextView.setText(String.valueOf(x));
		gyroscopeYTextView.setText(String.valueOf(y));
		gyroscopeZTextView.setText(String.valueOf(z));
	}

	
}